package com.dengqi.mymodule.activity.unittest;

import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import com.dengqi.mymodule.R;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.clearText;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

/**
 * Created by dell on 2019/2/21
 */
@RunWith(AndroidJUnit4.class)
@LargeTest
public class UnitTestActivityTest {

    @Rule
    public ActivityTestRule<UnitTestActivity> mActivityRule = new ActivityTestRule<>(UnitTestActivity.class);

    @Test
    public void testSubmit() {
//        String operandOne, String operandTwo, String expectedResult
        String operandOne = "2.0";
        String operandTwo = "2.0";
        String expectedResult = "4.0";

        // 指定输入框中输入文本，同时关闭键盘
        //Step 1:定位View
//        ViewInteraction etNum1 = onView(withId(R.id.et_num1));
        //Step 2:执行操作-输入文字
//        etNum1.perform(typeText(operandOne));
        //Step 3:判断结果
//        etNum1.check(matches(withText(result)));
        onView(withId(R.id.et_num1)).perform(clearText());
        onView(withId(R.id.et_num2)).perform(clearText());
        onView(withId(R.id.et_num1)).perform(typeText(operandOne), closeSoftKeyboard());
        onView(withId(R.id.et_num2)).perform(typeText(operandTwo), closeSoftKeyboard());
        // 通过文字定位按钮 执行点击事件
        onView(withText("计算")).perform(click());
        // 获取文本框中显示的结果
        onView(withId(R.id.tv_result)).check(matches(withText(expectedResult)));
        //执行点击返回按钮事件
//        pressBack();
    }

}